package com.wnhz.work.jy.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * -----客服
 */

public class CustomService extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "CustomService";
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 5555;

    private TextView title;
    private TextView tv_phone, tv_QQ, tv_call, tv_copy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_service);

        initData();
        initView();
        getData();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("客服");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        tv_phone = (TextView) findViewById(R.id.tv_service_phoneNum);
        tv_QQ = (TextView) findViewById(R.id.tv_service_QQ);
        tv_call = (TextView) findViewById(R.id.tv_service_call);
        tv_copy = (TextView) findViewById(R.id.tv_service_copy);

        tv_call.setOnClickListener(this);
        tv_copy.setOnClickListener(this);

    }

    private void initData() {
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        TextView telephone_number;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //拨打电话
            case R.id.tv_service_call:
                dialog = new Dialog(this, R.style.MyDialog);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.dialog);
                dialog.findViewById(R.id.call_off).setOnClickListener(this);
                dialog.findViewById(R.id.call_up).setOnClickListener(this);
                telephone_number = (TextView) dialog.findViewById(R.id.telephone_number);
                telephone_number.setText(tv_phone.getText().toString());

                dialog.show();

                break;

            //拨打电话
            case R.id.call_up:
                String inputStr = tv_phone.getText().toString();
                if (inputStr.trim().length() != 0) {
                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.CALL_PHONE)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(this,
                                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_CALL_PHONE);

                    } else {
                        Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + inputStr));
//                    Intent phoneIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + inputStr));
                        startActivity(phoneIntent);
                    }


                }
                break;

            //取消
            case R.id.call_off:
                dialog.dismiss();
                break;


            case R.id.tv_service_copy:
                MyToast("复制成功");
                ClipboardManager copy = (ClipboardManager) CustomService.this
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setText(QQ);
                break;

            default:
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phoneNumber));
//                    Intent phoneIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + inputStr));
                startActivity(phoneIntent);
            } else {
                MyToast("您的手机已禁止拨打电话的权限");
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    private String phoneNumber;
    private String QQ;

    private void getData() {
        showDialog();
        XUtil.Post(ConfigUtil.KEFU, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                try {
                    JSONObject json = new JSONObject(result);
                    phoneNumber = json.optString("phoneNumber");
                    QQ = json.optString("QQ");
                    setText();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    private void setText() {
        tv_phone.setText(phoneNumber);
        tv_QQ.setText(QQ);
    }
}
